package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hotel.domain.*;
import com.ruoyi.hotel.domain.bo.HmCheckinBo;
import com.ruoyi.hotel.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmBillBo;
import com.ruoyi.hotel.domain.vo.HmBillVo;
import com.ruoyi.hotel.service.IHmBillService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmBillServiceImpl implements IHmBillService {

    private final HmBillMapper baseMapper;

    private final HmRoomMapper hmRoomMapper;
    private final HmRoomTypeMapper hmRoomTypeMapper;
    private final HmServiceRequestMapper hmServiceRequestMapper;
    private final HmTaskMapper hmTaskMapper;
    private final HmSupplyMapper hmSupplyMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmBillVo queryById(Long billId){
        return baseMapper.selectVoById(billId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmBillVo> queryPageList(HmBillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmBill> lqw = buildQueryWrapper(bo);
        Page<HmBillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmBillVo> queryList(HmBillBo bo) {
        LambdaQueryWrapper<HmBill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmBill> buildQueryWrapper(HmBillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmBill> lqw = Wrappers.lambdaQuery();

        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmBillBo bo) {
        HmBill add = BeanUtil.toBean(bo, HmBill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBillId(add.getBillId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmBillBo bo) {
        HmBill update = BeanUtil.toBean(bo, HmBill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmBill entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<HmBill> getMyBill(Long id){
        QueryWrapper<HmBill> qw=new QueryWrapper<>();
        qw.eq("customer_id",id);
        return baseMapper.selectList(qw);
    }

    @Override
    public void createBill(@RequestBody HmCheckinBo hmCheckin,Long id){

        HmBillBo hmBillBo=new HmBillBo();
        hmBillBo.setCheckinId(hmCheckin.getCheckinId());
        hmBillBo.setCustomerId(hmCheckin.getCustomerId());

        HmRoom hmRoom=hmRoomMapper.selectById(hmCheckin.getRoomId());
        HmRoomType hmRoomType=hmRoomTypeMapper.selectById(hmRoom.getRoomType());
        double priceRoom=hmRoomType.getPrice();

        LocalDate localDate1 = hmCheckin.getCheckinTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate localDate2 = hmCheckin.getCheckoutTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        double daysDifference = (double) ChronoUnit.DAYS.between(localDate1, localDate2);

        hmBillBo.setRoomCost(priceRoom*daysDifference);

        QueryWrapper<HmServiceRequest> qw=new QueryWrapper<>();
        qw.eq("checkin_id",hmBillBo.getCheckinId());
        qw.eq("is_done","y");
        List<HmServiceRequest> list=hmServiceRequestMapper.selectList(qw);

        double serviceCost=0;

        for (HmServiceRequest element : list) {

            QueryWrapper<HmTask> hmTaskQueryWrapper=new QueryWrapper<>();
            hmTaskQueryWrapper.eq("service_request_id",element.getServiceRequestId());
            HmTask hmTask =hmTaskMapper.selectOne(hmTaskQueryWrapper);

            double priceSupply=hmSupplyMapper.selectById(hmTask.getSupplyId()).getPrice();

            serviceCost+=priceSupply* hmTask.getSupplyAmount();


        }

        hmBillBo.setServiceCost(serviceCost);

        hmBillBo.setBillActual(hmBillBo.getRoomCost()+hmBillBo.getServiceCost());

        hmBillBo.setWorkerId(id);

        this.insertByBo(hmBillBo);

    }
}
